package br.com.sgh.util;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
    public static final String HIBERNATE_SESSION = "hibernate_session";
    private static SessionFactory sessionFactory;

    static {
        try {
            System.out.println(">>>>> Tentando abrir a conexão <<<<<<<");
            Configuration configuration = new Configuration().configure();
            ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            System.out.println(">>>>>> Conectado com sucesso <<<<<<<");
        } catch (HibernateException e) {
            System.out.println(">>>>>>> Não Conectou <<<<<<<"+e.getMessage());
            throw new ExceptionInInitializerError(e);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}
